﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cesave.Domain.PermisosModule.Entities;
using Cesave.Infrastructure.Data.Core;
using Cesave.Domain.PermisosModule.Contracts.AreaSecciones;
using Cesave.Infrastructure.Data.PermisosModule.Context;
using Cesave.Infrastructure.CrossCutting.Logging;
using Cesave.Domain.Core.Specification;
using System.Data.Objects;

namespace Cesave.Infrastructure.Data.PermisosModule.Repositories
{
	public class AreaSeccionRepository
		: GenericRepositoryEx<AreaSeccion>, IAreaSeccionRepository
	{
		#region Constructor

		/// <summary>
		/// 
		/// </summary>
		/// <param name="context"></param>
		/// <param name="traceManager"></param>
		public AreaSeccionRepository(IPermisosModuleContext context, ITraceManager traceManager)
			: base(context, traceManager)
		{
		}

		#endregion

		#region IAreaSeccionRepository Members

		/// <summary>
		/// 
		/// </summary>
		/// <param name="juntaSpecification"></param>
		/// <param name="areaSpecification"></param>
		/// <returns></returns>
		public IEnumerable<AreaSeccion> FindAreaSeccionesByJuntaAndArea(AreaSeccionesByJuntaSpecification juntaSpecification, AreaSeccionesByAreaSpecification areaSpecification)
		{
			if (juntaSpecification == null)
				throw new ArgumentNullException("juntaSpecification");
			if (areaSpecification == null)
				throw new ArgumentNullException("areaSpecification");
			Specification<AreaSeccion> specification = new TrueSpecification<AreaSeccion>();
			specification &= juntaSpecification;
			specification &= areaSpecification;
			IPermisosModuleContext context = StoreContext as IPermisosModuleContext;
			return from area_seccion in context.AreaSecciones.Where(specification.SatisfiedBy()).Include("Seccion")
				   join area in context.Areas on area_seccion.AreaGuid equals area.AreaGuid
				   join seccion in context.Secciones on area_seccion.SeccionGuid equals seccion.SeccionGuid
				   orderby seccion.SeccionId
				   select area_seccion;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="specification"></param>
		/// <returns></returns>
		public IEnumerable<AreaSeccion> FindAreaSeccionesForExpedicion(AreaSeccionesForExpedicionSpecification specification)
		{
			if (specification == null)
				throw new ArgumentNullException("specification");
			IPermisosModuleContext context = StoreContext as IPermisosModuleContext;
			return from area_seccion in context.AreaSecciones.Where(specification.SatisfiedBy()).Include("Seccion")
				   select area_seccion;
		}

		#endregion
	}
}
